package util

import (
	"github.com/spf13/viper"
	"go_server/zinx/zlog"
	"path"
	"runtime"
)

var projectConfigPath = path.Dir(getCurrentPath() + "/../") + "/conf/"

func getCurrentPath() string {
	_, fileName, _, ok := runtime.Caller(0)
	if !ok {
		zlog.Panic("get current filePath err!!!")
	}
	return path.Dir(fileName)
}

func CreateConfig(fileName string) (*viper.Viper, string) {
	config := viper.New()
	configPath := projectConfigPath
	config.AddConfigPath(configPath)
	config.SetConfigName(fileName)
	config.SetConfigType("yaml")

	configFile := configPath + fileName + ".yaml"

	if err := config.ReadInConfig(); err != nil {
		if _, ok := err.(viper.ConfigFileNotFoundError); ok {
			zlog.Panicf("找不到配置文件: %s", configFile)
		} else {
			zlog.Panicf("解析配置文件 %s 出错: %v", configFile, err)
		}
	}

	return config, configFile
}
